#include "Debug.h"
#include "UvBackup.h"


UvBackup::UvBackup(MSelectionList _currSel, Options* opts)
{
	currSel = _currSel;
	extractVerts();
	for(int i=0; i<allVerts.size(); i++)
	{
		allVerts.at(i)->reset();
		recordUVsForObject(allVerts.at(i));		
	}
}


UvBackup::~UvBackup(void)
{
}

void UvBackup::applyBackup()
{
	for(int i=0; i<objectUvs.size(); i++)	
		objectUvs.at(i)->verts->setUVs(objectUvs.at(i)->uArray, objectUvs.at(i)->vArray, objectUvs.at(i)->faceIDs);
}

void UvBackup::extractVerts()
{
	MObject component;
	MDagPath node;

	for(uint i=0; i<currSel.length(); i++)
	{
		currSel.getDependNode(i, component);
		currSel.getDagPath(i, node, component);
		MItMeshVertex* vertexIt = new MItMeshVertex(node, component);
		allVerts.push_back(vertexIt);
	}
}

void UvBackup::recordUVsForObject(MItMeshVertex* verts)
{
	while(!verts->isDone())
	{
		UvStorage* uvStor = new UvStorage();
		verts->getUVs(uvStor->uArray, uvStor->vArray, uvStor->faceIDs);	
		objectUvs.push_back(uvStor);
		verts->next();
	}	
}

